package com.jvtd.eyl.ui.main.home.search;


import android.text.TextUtils;

import com.jvtd.eyl.R;
import com.jvtd.eyl.base.BasePresenter;
import com.jvtd.eyl.bean.binding.PeriodicalBean;
import com.jvtd.eyl.data.DbManager;
import com.jvtd.eyl.rxjava.JvtdRxSchedulers;
import com.jvtd.rxjava.JvtdObserverSubscriber;

import java.util.List;

import javax.inject.Inject;

import io.reactivex.disposables.CompositeDisposable;

/**
 * 搜索期刊
 *
 * 作者:chenlei
 * 时间:2018/11/14 5:33 PM
 */
public class SearchPresenter<V extends SearchMvpView> extends BasePresenter<V> implements SearchMvpPresenter<V>
{
  @Inject
  public SearchPresenter(DbManager dbManager, CompositeDisposable disposable)
  {
    super(dbManager, disposable);
  }

  @Override
  public void searchPeriodicalList(String searchInfo) {
    if (!isAttachView()) return;
    if (TextUtils.isEmpty(searchInfo))
    {
      getMvpView().showMessage(R.string.search_info_error);
      return;
    }
    getMvpView().showLoading();
    getCompositeDisposable().add(getDbManager().searchPeriodicalList(searchInfo)
    .compose(JvtdRxSchedulers.handleObservableResult())
    .subscribeWith(new JvtdObserverSubscriber<List<PeriodicalBean>>(getMvpView())
    {
      @Override
      public void onNext(List<PeriodicalBean> periodicalBeans)
      {
        super.onNext(periodicalBeans);
        if (isAttachView())
          getMvpView().searchPeriodicalListSuccess(periodicalBeans);
      }

      @Override
      public void onError(Throwable e)
      {
        super.onError(e);
        if (isAttachView())
          getMvpView().searchPeriodicalListFailed();
      }
    }));
  }
}
